package timgmbh.monopoly.objects;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import timgmbh.monopoly.services.DatabaseService;

public class Field extends DatabaseObject {
	
	private String name;
	private int rentSingle;
	private int rent1;
	private int rent2;
	private int rent3;
	private int rent4;
	private int rentHotel;
	private int price;
	private int buildingPrice;
	private String kind;
	
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	// CONSTRUCTORS
	
	public Field ()
	{
		super("FIELD","ID");
	}
	
	public Field(int id) {
		super("FIELD","ID");
		
		load(id);
	}
	
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	// LOAD
	
	public boolean load(int id)
	{
		super.load(id);
		
		PreparedStatement stmnt = null;
		ResultSet res = null;
		
		try {
			stmnt = DatabaseService.getConn().prepareStatement("SELECT * FROM FIELDTYPE WHERE ID = ?");
			stmnt.setInt(1, getTypeId());
			res = stmnt.executeQuery();
		
			if (res.next()) {
				name = res.getString("NAME");
				kind = res.getString("KIND").toUpperCase();
				rentSingle = res.getInt("RENT_SINGLE");
				rent1 = res.getInt("RENT_1");
				rent2 = res.getInt("RENT_2");
				rent3 = res.getInt("RENT_3");
				rent4 = res.getInt("RENT_4");
				rentHotel = res.getInt("RENT_HOTEL");
				price = res.getInt("PRICE");
				buildingPrice = res.getInt("BUILDING_PRICE");
			}
		} catch (Exception e) {
			System.out.println("Error: " + e.getClass() + ": " + e.getMessage());
			return false;
		} finally {
			try {
				res.close();
				stmnt.close();
			} catch (Exception e) {
				System.out.println("Error: " + e.getClass() + ": " + e.getMessage());
				return false;
			}
		}
		
		return true;
	}
	
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	// GETTER
	
	public Player getOwner() {
		if (getValue("OWNER") == null)
			return null;
		else
			return new Player((int)getValue("OWNER"));
	}
	public Game getGame() {
		return new Game((int)getValue("GAME"));
	}
	public int getTypeId() {
		return (int)getValue("TYPE");
	}
	public int getBuildings() {
		return (int)getValue("BUILDINGS");
	}
	public String getName() {
		return name;
	}
	public String getKind() {
		return kind;
	}
	public int getPrice() {
		return price;
	}
	public int getBuildingPrice() {
		return buildingPrice;
	}
	public int getRent() {
		if (getBuildings() == 0) return rentSingle;
		else if (getBuildings() == 1) return rent1;
		else if (getBuildings() == 2) return rent2;
		else if (getBuildings() == 3) return rent3;
		else if (getBuildings() == 4) return rent4;
		else if (getBuildings() == 5) return rentHotel;
		else return -1;
	}
	public int getMaxBuildings() {
		
		int maxBuildings = this.getBuildings() + 1;
		PreparedStatement stmnt = null;
		ResultSet res = null;
		try {
			stmnt = DatabaseService.getConn().prepareStatement("SELECT FIELD.ID FROM FIELD, FIELDTYPE WHERE FIELD.TYPE = FIELDTYPE.ID AND FIELDTYPE.KIND = ? AND FIELD.ID <> ? AND FIELD.GAME = ?");
			stmnt.setString(1, getKind());
			stmnt.setInt(2, getID());
			stmnt.setInt(3, this.getGame().getID());
			res = stmnt.executeQuery();
		
			Field temp = new Field();
			while (res.next()) {
				temp.load(res.getInt("ID"));
				
				Player tempOwner = temp.getOwner();
				Player thisOwner = this.getOwner();
				
				if (tempOwner.getID() != thisOwner.getID()) maxBuildings = 0;
				if (temp.getBuildings() < maxBuildings - 1) maxBuildings = temp.getBuildings() + 1;
			}
			if (maxBuildings > 5) maxBuildings = 5;
			
			return maxBuildings;
		} catch (Exception e) {
			System.out.println("Error: " + e.getClass() + ": " + e.getMessage());
			return 0;
		} finally {
			try {
				res.close();
				stmnt.close();
			} catch (Exception e) {
				System.out.println("Error: " + e.getClass() + ": " + e.getMessage());
				return 0;
			}
		}
	}
	
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	// SETTER
	
	public boolean setOwner(Player val) {
		if (val == null)
			return setValue("OWNER", null);
		else
			return setValue("OWNER", val.getID());
	}
	public boolean setGame(Game val) {
		return setValue("GAME", val.getID());
	}
	public boolean setTypeId(int val) {
		return setValue("TYPE", val);
	}
	public boolean setBuildings(int val) {
		return setValue("BUILDINGS", val);
	}
}
